use crate::sql;
extern crate gtk;
use gtk::prelude::*;
use gtk::{Application,ApplicationWindow, ButtonsType, DialogFlags, MessageType, MessageDialog, Window,Button};
use gtk::AboutDialog;
use glib::{clone};
use std::path::Path;
use std::env;
use std::str::Split;
pub fn main() {
   let app = Application::builder()
    .application_id("org.example.HelloWorld")
    .build();
    app.connect_activate(|app| {
        let win = ApplicationWindow::builder()
            .application(app)
            .default_width(320)
            .default_height(200)
            .title("Hello, World!")
            .build();
        let outer_box = gtk::Box::new(gtk::Orientation::Vertical, 0);
        let entry: gtk::Entry = gtk::Entry::new();
        entry.set_size_request(500, 15);
        entry.set_margin_start(10);
        entry.set_margin_end(10);
        entry.set_margin_top(10);
        entry.set_max_width_chars(20);
        entry.set_text("2024-01-21 06:38:06");
        let button_jia = Button::with_label("+");
        button_jia.set_size_request(300, 20);
        button_jia.set_margin_start(30);
        button_jia.set_margin_end(30);
        button_jia.set_margin_top(30);
        button_jia.connect_clicked(glib::clone!(@weak win, @weak entry => move |_| {
            let text = String::from(entry.text().to_string());
            let cur_exe=std::env::current_exe().unwrap();
            let root=cur_exe.parent().unwrap();
            let dbfile=root.join("data.db");
            let constr=format!("{}",dbfile.display());
            let conn = rusqlite::Connection::open(constr).unwrap();
            sql::change_one(conn,text,1.0/1.005);
        }));
        let button = Button::with_label("about");
        button.set_size_request(300, 20);
        button.set_margin_start(30);
        button.set_margin_end(30);
        button.set_margin_top(30);
        button.connect_clicked(|_| {
            eprintln!("Clicked!");
            AboutDialog::new().present();
        });
        let button2 = Button::with_label("hello");
        button2.set_size_request(300, 20);
        button2.set_margin_start(30);
        button2.set_margin_end(30);
        button2.set_margin_top(30);
        // button2.connect_clicked(move |button2| {
        button2.connect_clicked(glib::clone!(@weak win, @weak entry => move |_| {
            // Set the label to "Hello World!" after the button has been clicked on
            entry.set_text("Hello World!");
        }));
        // button.connect_clicked(move|_| {
        //     entry.set_text("hello")
        //     // let buffer = entry.buffer();
        //     // let content = buffer.text();
        //     // let todo_object = TodoObject::new(false, content);
        //     // model.append(&todo_object);
        //     // buffer.set_text("");
        // });
        let logtext = gtk::TextView::new();
        logtext.set_halign(gtk::Align::Start); // 10 pixel right margin
        logtext.set_editable(false);
        logtext.set_size_request(950, 300);
        // logtext.get_buffer();//().unwrap().set_text("");
        logtext.set_margin_start(10);
        logtext.set_margin_end(10);
        logtext.set_margin_top(20);
        outer_box.append(&entry);
        outer_box.append(&button_jia);
        outer_box.append(&button2);
        outer_box.append(&logtext);
        
        outer_box.append(&button);
        win.set_child(Some(&outer_box));
        win.present();
        });

    app.run();
}